package com.sptpc.demo1.service.impl;

import com.sptpc.demo1.model.Category;
import com.sptpc.demo1.model.Drink;
import com.sptpc.demo1.repository.CategoryRepository;
import com.sptpc.demo1.repository.DrinkRepository;
import com.sptpc.demo1.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class DrinkJpaService implements IDrinkService {
    @Autowired
    public DrinkRepository drinkRepository;
    @Autowired
    public CategoryRepository catgoryRepository;

    @Override
    public Page<Drink> getAllDrinks(PageRequest pageRequest) {
        return drinkRepository.findAll(pageRequest);
    }

    @Override
    public void addNewDrink(Drink drink, Integer categoryId) {
        Category category = catgoryRepository.findById(categoryId).orElse(null);
        drink.setCategory(category);
        drinkRepository.save(drink);
    }

    @Override
    public void deleteDrinkById(Integer id) {
        drinkRepository.deleteById(id);
    }

    @Override
    public Drink getDrinkById(Integer id) {
        return drinkRepository.findById(id).orElse(null);
    }

    @Override
    public void setDrink(Drink drink, Integer id) {
        Category category = catgoryRepository.findById(id).orElse(null);
        drink.setCategory(category);
        drinkRepository.save(drink);
    }
}
